package com.coolgor.axnut.service.auth.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.auth.OperationTraceDao;
import com.coolgor.axnut.dto.auth.OperationTraceExecution;
import com.coolgor.axnut.entity.auth.OperationTrace;
import com.coolgor.axnut.enums.auth.OperationTraceStateEnum;
import com.coolgor.axnut.exceptions.auth.OperationTraceOperationException;
import com.coolgor.axnut.service.auth.OperationTraceService;
import com.coolgor.axnut.util.PageCalculator;



@Service
public class OperationTraceServiceImpl implements OperationTraceService {
	private static Logger log = LoggerFactory.getLogger(OperationTraceServiceImpl.class);
	
	@Autowired
	private OperationTraceDao operationTraceDao;
	
	@Override
	public OperationTraceExecution touch(OperationTrace operationTrace) {
		long recordID = operationTrace.getRecordID();
		if (!"".equals(recordID) && recordID>0) {
			//如果记录ID不为零，或空值，请执行更新操作
			operationTrace.setLastTime(new Date());
			return this.modifyOperationTrace(operationTrace);
		} else {
			//如果记录ID为零或者是空值，请执行插入操作
			operationTrace.setActionTime(new Date());
			operationTrace.setLastTime(new Date());
			return this.addOperationTrace(operationTrace);
		}
	}

	@Override
	public OperationTraceExecution getOperationTraceList(OperationTrace operationTraceCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<OperationTrace> operationTraceList = operationTraceDao.queryOperationTraceList(operationTraceCondition, rowIndex, pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = operationTraceDao.queryOperationTraceCount(operationTraceCondition);
		OperationTraceExecution result = new OperationTraceExecution();
		if (operationTraceList != null) {
			result.setState(OperationTraceStateEnum.SUCCESS.getState());
			result.setStateInfo(OperationTraceStateEnum.SUCCESS.getStateInfo());
			result.setOperationTraceList(operationTraceList);
			result.setCount(count);
		} else {
			result.setState(OperationTraceStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public OperationTraceExecution addOperationTrace(OperationTrace operationTrace) throws OperationTraceOperationException {
		// 空值判断
		if (operationTrace == null) {
			return new OperationTraceExecution(OperationTraceStateEnum.INNER_ERROR);
		}
		int effectedNum;
		try {
			// 赋初始值
			//loginInfo.setLoginTime(new Date());
			// 添加信息
			effectedNum = operationTraceDao.insertOperationTrace(operationTrace);
			if (effectedNum <= 0) {
				throw new OperationTraceOperationException("用户操作信息记录失败");
			} 
		} catch (Exception e) {
			throw new OperationTraceOperationException("Add OperationTrace error:" + e.getMessage());
		}
		return new OperationTraceExecution(OperationTraceStateEnum.SUCCESS, operationTrace);
	}

	@Override
	@Transactional
	public OperationTraceExecution modifyOperationTrace(OperationTrace operationTrace) throws OperationTraceOperationException {
		// 空值判断
		if (operationTrace == null) {
			return new OperationTraceExecution(OperationTraceStateEnum.INNER_ERROR);
		}
		int effectedNum;
		try {
			// 更新操作
			effectedNum = operationTraceDao.updateOperationTrace(operationTrace);
			if (effectedNum <= 0) {
				throw new OperationTraceOperationException("用户操作痕迹信息更新失败");
			} 
		} catch (Exception e) {
			throw new OperationTraceOperationException("Modify OperationTrace error:" + e.getMessage());
		}
		return new OperationTraceExecution(OperationTraceStateEnum.SUCCESS, operationTrace);
	}

	@Override
	@Transactional
	public OperationTraceExecution removeOperationTrace(OperationTrace operationTrace) throws OperationTraceOperationException {
		// 空值判断
		if (operationTrace == null) {
			return new OperationTraceExecution(OperationTraceStateEnum.INNER_ERROR);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = operationTraceDao.deleteOperationTrace(operationTrace);
			if (effectedNum <= 0) {
				throw new OperationTraceOperationException("用户操作痕迹信息删除失败");
			} 
		} catch (Exception e) {
			throw new OperationTraceOperationException("Delete User OperationTrace error:" + e.getMessage());
		}
		return new OperationTraceExecution(OperationTraceStateEnum.SUCCESS, operationTrace);
	}

	@Override
	@Transactional
	public OperationTraceExecution batchRemoveOperationTrace(List<OperationTrace> operationTraceList) throws OperationTraceOperationException {
		// 空值判断
		if (operationTraceList == null) {
			return new OperationTraceExecution(OperationTraceStateEnum.INNER_ERROR);
		}
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<operationTraceList.size(); i++) {
			recordIDList.add(operationTraceList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = operationTraceDao.batchDeleteOperationTrace(recordIDList);
			if (effectedNum <= 0) {
				throw new OperationTraceOperationException("用户操作信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new OperationTraceOperationException("Batch Delete OperationTrace error:" + e.getMessage());
		}
		OperationTraceExecution exe = new OperationTraceExecution(OperationTraceStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		exe.setCount(effectedNum);
		return exe;
	}

}
